<?php

namespace App\Store\Services\Message;

use App\Admin\Models\SiteCompany;

class Lianlu
{
    public static function postCurl($json, $url, $second = 5)
    {
        $ch = curl_init();

        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER,['Content-Type:application/json']);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //获取的信息以文件流的形式返回，而不是直接输出
        curl_setopt($ch, CURLOPT_HEADER, false);

        if (false !== strpos($url, "https")) {
            // 证书
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  false);
        }

        $response = curl_exec($ch); // 已经获取到内容，没有输出到页面上。

        curl_close($ch);
        return $response;
    }

    //发送短信
    public static function NormalSend($mobile, $content, $companyinfo)
    {
        $url = "https://apis.shlianlu.com/sms/trade/normal/send";
        // $params['MchId'] = env('LIANLU_MCHID');
        // $params['AppId'] = env('LIANLU_APPID');
        // $params['SignName'] = '【珠小宝】';
        // $appKey = env('LIANLU_APPKEY');
        $params['MchId'] = $companyinfo->mchid;
        $params['AppId'] = $companyinfo->appid;
        $params['SignName'] =!empty($companyinfo->signname)?'【'.$companyinfo->signname.'】': '【珠小宝】';
        $appKey = $companyinfo->appkey;


        $params['Version'] = '1.1.0';
        $params['Type'] = '1';
        $params['PhoneNumberSet'] = $mobile;
        $params['SessionContext'] = $content;
        $params['TimeStamp'] = self::getMillisecond();
        $params['SignType'] = 'MD5';
        $params['Signature'] = self::MakeSign($params, $appKey);
        $json = json_encode($params);
        return self::postCurl($json, $url);
    }

    /**
     * 生成签名
     * @param $config
     * @param $appKey
     * @return string
     */
    public static function MakeSign($params, $appKey)
    {
        //签名步骤一：按字典序排序参数
        ksort($params);
        $string = self::ToUrlParams($params);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $appKey;
        //签名步骤三：MD5加密或者HMAC-SHA256
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        return strtoupper($string);
    }

    /**
     * 格式化参数格式化成url参数
     */
    public static function ToUrlParams($params)
    {
        $buff = "";
        $noSignKey = array("Signature", "ContextParamSet", "TemplateParamSet", "SessionContextSet", "PhoneNumberSet", "SessionContext", "PhoneList", "phoneSet");
        foreach ($params as $k => $v)
        {
            if(!in_array($k, $noSignKey) && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }
        return trim($buff, "&");
    }

    /**
     * 获取毫秒级别的时间戳
     */
    public static function getMillisecond()
    {
        //获取毫秒的时间戳
        $time = explode ( " ", microtime () );
        $time = $time[1] . ($time[0] * 1000);
        $time2 = explode( ".", $time );
        return $time2[0];
    }
}
